package com.example.mysports.ui

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.mysports.R
import com.example.mysports.data.LocalSportsDataProvider
import com.example.mysports.model.Sport

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun SportList(
    sportUiState: SportUiState,
    onCardClick: (Sport) -> Unit,
    modifier: Modifier = Modifier,
    isFullScreen: Boolean = false
) {
    val transitionState = remember {
        MutableTransitionState(false).apply {
            targetState = true
        }
    }
    AnimatedVisibility(
        visibleState = transitionState,
        enter = fadeIn(
            animationSpec = spring(dampingRatio = Spring.DampingRatioLowBouncy)
        ),
        exit = fadeOut(),
        modifier = modifier
    ) {
        val sports: List<Sport> = sportUiState.sports
        LazyColumn {
            itemsIndexed(sports) { index, sport ->
                SportListItem(
                    sport = sport,
                    onItemClick = {
                        onCardClick(sport)
                    },
                    selected = isFullScreen && sport.id == sportUiState.currentSport.id,
                    modifier = Modifier
                        .padding(vertical = dimensionResource(id = R.dimen.padding_small))
                        .animateEnterExit(
                            enter = slideInVertically(
                                animationSpec = spring(
                                    dampingRatio = Spring.DampingRatioLowBouncy,
                                    stiffness = Spring.StiffnessVeryLow
                                ),
                                initialOffsetY = { it * (index + 1) }
                            )
                        )
                )
            }
        }
    }
}

@Composable
private fun SportListItem(
    sport: Sport,
    onItemClick: () -> Unit,
    selected: Boolean,
    modifier: Modifier = Modifier
) {
    Card(
        colors = CardDefaults.cardColors(
            if (selected) {
                MaterialTheme.colorScheme.primaryContainer
            } else {
                MaterialTheme.colorScheme.surfaceVariant
            }
        ),
        shape = RoundedCornerShape(dimensionResource(id = R.dimen.card_corner_radius)),
        onClick = onItemClick,
        modifier = modifier
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .size(dimensionResource(id = R.dimen.card_image_height))
        ) {
            Image(
                painter = painterResource(id = sport.icon),
                contentDescription = "Sport Icon"
            )
            Column(
                modifier = Modifier
                    .padding(
                        vertical = dimensionResource(id = R.dimen.padding_small),
                        horizontal = dimensionResource(id = R.dimen.padding_medium)
                    )
                    .weight(1f)
            ) {
                Text(
                    text = stringResource(id = sport.title),
                    style = MaterialTheme.typography.titleMedium,
                    modifier = Modifier.padding(bottom = 4.dp)
                )
                Text(
                    text = stringResource(id = sport.subtitle),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.secondary,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 3
                )
                Spacer(Modifier.weight(1f))
                Row {
                    Text(
                        text = pluralStringResource(
                            id = R.plurals.cart_players,
                            sport.playerCount,
                            sport.playerCount
                        ),
                        style = MaterialTheme.typography.bodySmall
                    )
                    Spacer(modifier = Modifier.weight(1f))
                    if (sport.olympic) {
                        Text(
                            text = stringResource(id = R.string.cart_olympic),
                            style = MaterialTheme.typography.labelMedium
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun SportListAndDetails(
    sportUiState: SportUiState,
    onCardClick: (Sport) -> Unit,
    contentPadding: PaddingValues,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        SportList(
            sportUiState = sportUiState,
            onCardClick = onCardClick,
            modifier = Modifier
                .padding(contentPadding)
                .weight(2f),
            isFullScreen = true
        )
        SportDetails(
            sport = sportUiState.currentSport,
            contentPadding = contentPadding,
            modifier = Modifier.weight(3f)
        )
    }
}

//@Preview(showBackground = true, widthDp = 1000)
//@Composable
//fun SportListAndDetailsPreview() {
//    val sportUiState = SportUiState(
//        sports = LocalSportsDataProvider.getSportsList(),
//        currentSport = LocalSportsDataProvider.getSportsList()[1]
//    )
//    SportListAndDetails(
//        sportUiState = sportUiState,
//        onCardClick = { },
//        contentPadding = PaddingValues(0.dp)
//    )
//}

@Preview(showBackground = true)
@Composable
fun SportListPreview() {
    val sportUiState = SportUiState(
        sports = LocalSportsDataProvider.getSportsList(),
        currentSport = LocalSportsDataProvider.getSportsList()[1]
    )
    SportList(
        sportUiState,
        onCardClick = { },
        modifier = Modifier.padding(16.dp)
    )
}